from django.db import models
from django.contrib.auth import get_user_model
from apps.source.models import ProductSource
from apps.base.models import *


# 需求采集
class DemandGathering(models.Model):
    A001 = models.CharField(primary_key=True, max_length=13, verbose_name='需求申请单号')
    A002 = models.DateTimeField(auto_now_add=True, null=True, verbose_name='需求申请日期')
    F001 = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, verbose_name='业务员')
    F002 = models.ForeignKey(Customer, on_delete=models.CASCADE, null=True,verbose_name='客户')

    D001 = models.CharField(max_length=10, null=True, blank=True, verbose_name='行业类别')
    D002 = models.CharField(max_length=20, null=True, blank=True, verbose_name='被贴产品名称')
    D003 = models.CharField(max_length=10, null=True, blank=True, verbose_name='被贴产品材质')
    D004 = models.CharField(max_length=20, null=True, blank=True, verbose_name='被贴表面处理')
    D005 = models.CharField(max_length=15, null=True, blank=True, verbose_name='成型用途')
    D006 = models.CharField(max_length=10, null=True, blank=True, verbose_name='客户终端')
    D007 = models.CharField(max_length=10, null=True, blank=True, verbose_name='保护膜用途')
    D008 = models.CharField(max_length=4, null=True, blank=True, verbose_name='运输方式')

    D009 = models.CharField(max_length=10, null=True, blank=True, verbose_name='测试温度')
    D010 = models.CharField(max_length=10, null=True, blank=True, verbose_name='测试湿度')
    D011 = models.CharField(max_length=10, null=True, blank=True, verbose_name='测试时长')
    D012 = models.CharField(max_length=10, null=True, blank=True, verbose_name='测试板材')
    D013 = models.CharField(max_length=20, null=True, blank=True, verbose_name='寻膜原因')

    E001 = models.CharField(max_length=20, null=True, blank=True, verbose_name='保护膜种类')
    E002 = models.CharField(max_length=20, null=True, blank=True, verbose_name='类别')
    E003 = models.CharField(max_length=20, null=True, blank=True, verbose_name='胶系')
    E004 = models.CharField(max_length=20, null=True, blank=True, verbose_name='洁净度要求')
    E005 = models.CharField(max_length=20, null=True, blank=True, verbose_name='结构')
    E006 = models.CharField(max_length=20, null=True, blank=True, verbose_name='厚度')
    E007 = models.CharField(max_length=50, null=True, blank=True, verbose_name='粘度范围g')

    E008 = models.CharField(max_length=20, null=True, blank=True, verbose_name='颜色')
    E009 = models.CharField(max_length=30, null=True, blank=True, verbose_name='特殊要求')

    G001 = models.CharField(max_length=40, null=True, blank=True, verbose_name='规格')
    G002 = models.CharField(max_length=10, null=True, blank=True, verbose_name='每月使用量')
    G003 = models.CharField(max_length=10, null=True, blank=True, verbose_name='期望单价')
    G004 = models.CharField(max_length=10, null=True, blank=True, verbose_name='月采购金额')

    G005 = models.CharField(max_length=10, null=True, blank=True, verbose_name='膜样运输说明')

    def __str__(self):
        return self.A001

    class Meta:
        verbose_name = '需求采集'
        verbose_name_plural = verbose_name


# 需求图片表（存放需求表中的图片）
class DemandImage(models.Model):
    Image = models.ImageField(upload_to='images/demand', null=True, verbose_name='图片')
    F001 = models.ForeignKey(DemandGathering, on_delete=models.CASCADE, verbose_name='对应需求单')

    class Meta:
        verbose_name = '需求采集表图片'
        verbose_name_plural = verbose_name


# 报价单
class Quotation(models.Model):
    A001 = models.CharField(max_length=12, primary_key=True, verbose_name='报价单编号')
    A002 = models.DateField(auto_now_add=True, verbose_name='报价时间')
    F001 = models.ForeignKey(Customer, on_delete=models.CASCADE, verbose_name='客户')
    F002 = models.ForeignKey(ProductName, on_delete=models.CASCADE, verbose_name='产品')
    F003 = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='报价人')

    B001 = models.CharField(max_length=5, null=True, verbose_name='形式')
    B002 = models.CharField(max_length=10, null=True, verbose_name='付款方式')
    B003 = models.CharField(max_length=10, null=True, verbose_name='账期')
    B004 = models.CharField(max_length=5, null=True, verbose_name='币种')
    B005 = models.FloatField(verbose_name='未税单价')
    B006 = models.CharField(max_length=5, null=True, verbose_name='单位')
    B007 = models.CharField(max_length=10, null=True, verbose_name='审核人')
    B008 = models.CharField(max_length=30, null=True, verbose_name='备注')

    def __str__(self):
        return self.A001

    class Meta:
        verbose_name = '报价单'
        verbose_name_plural = verbose_name


# 样品申请单
class SampleApply(models.Model):
    A002 = models.DateTimeField(auto_now_add=True, null=True,verbose_name='创建时间')

    F001 = models.CharField(max_length=10, null=True,verbose_name='填表人')
    F002 = models.ForeignKey(DemandGathering, null=True, on_delete=models.SET_NULL,verbose_name='需求编号')
    F003 = models.ForeignKey(ProductSource, null=True, on_delete=models.SET_NULL, blank=True,verbose_name='产品料号')

    B001 = models.IntegerField(null=True,verbose_name='宽度')
    B002 = models.IntegerField(null=True,verbose_name='长度')
    B003 = models.IntegerField(null=True,verbose_name='数量')
    B004 = models.CharField(max_length=5, null=True,blank=True,verbose_name='单位')
    B005 = models.CharField(max_length=20, null=True,blank=True,verbose_name='兰庆型号')
    B006 = models.CharField(max_length=10, null=True,blank=True,verbose_name='联系人')
    B007 = models.CharField(max_length=30, null=True,blank=True,verbose_name='收件地址')

    def __str__(self):
        return f'{self.F002}-{self.F003.pk}'

    class Meta:
        verbose_name = '样品申请单'
        verbose_name_plural = verbose_name